package org.example.ExperimentThree.ClientFile;

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class FileClient {
    private static Socket socket;
    private static DataInputStream dis;
    private static DataOutputStream dos;
//命令行选项操作
    public static void main(String[] args) throws IOException {
        connectToServer();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("> ");
            String command = scanner.nextLine();
            if (command.equals("ls")) {
                listFiles();
            } else if (command.startsWith("get ")) {
                String filename = command.substring(4);
                getFile(filename);
            } else if (command.startsWith("put ")) {
                String filename = command.substring(4);
                putFile(filename);
            } else if (command.equals("quit")) {
                quit();
                break;
            } else {
                System.out.println("Invalid command.");
            }
        }
        scanner.close();
    }
//连接主机操作
    private static void connectToServer() throws IOException {
        socket = new Socket("localhost", 58874);
        dis = new DataInputStream(socket.getInputStream());
        dos = new DataOutputStream(socket.getOutputStream());
        System.out.println("服务端连接成功.");
    }
//目录列表
    private static void listFiles() throws IOException {
        dos.writeUTF("ls");
        int count = dis.readInt();
        System.out.println(count + " files:");
        for (int i = 0; i < count; ++i) {
            String filename = dis.readUTF();
            long filesize = dis.readLong();
            System.out.println(filename + " (" + filesize + " bytes)");
        }
    }
//下载文件
    private static void getFile(String filename) throws IOException {
        dos.writeUTF("get " + filename);
        long filesize = dis.readLong();
        if (filesize == -1) {
            System.out.println(filename + " 文件不存在.");
            return;
        }
        FileOutputStream fos = new FileOutputStream("E:\\Java\\JavaExperimentOne\\downloadClient\\" + filename);
        fos.close();
        System.out.println("文件下载成功！");
    }
//上传文件
    private static void putFile(String filename) throws IOException {
        File file = new File("E:\\Java\\JavaExperimentOne\\downloadClient\\" + filename);
        if (!file.exists()) {
            System.out.println(filename + " 没有在客户端找到存在的文件.");
            return;
        }
        dos.writeUTF("put " + filename);
        FileInputStream fis = new FileInputStream(file);
        byte[] buffer = new byte[4096];
        int bytesRead;
        long totalBytesRead = 0;
        long startTime = System.currentTimeMillis();
        while ((bytesRead = fis.read(buffer)) != -1) {
            dos.write(buffer, 0, bytesRead);
            totalBytesRead += bytesRead;
            long elapsedTime = System.currentTimeMillis() - startTime;
            if (elapsedTime >= 1000) {
                double speed = (double) totalBytesRead / elapsedTime * 1000 / 1024;
                System.out.println("Uploaded " + totalBytesRead + " bytes (" + String.format("%.2f", speed) + " KB/s)");
                startTime = System.currentTimeMillis();
                totalBytesRead = 0;
            }
        }
        fis.close();
        System.out.println("Uploaded " + file.length() + " bytes in total.");
    }
//退出
    private static void quit() throws IOException {
        dos.writeUTF("quit");
        dis.close();
        dos.close();
        socket.close();
        System.out.println("断开连接.");
    }
}
